//
// Created by user on 20-3-19.
//

#include "Interrupt.h"

BuildIRQ(0x20)

BuildIRQ(0x21)

BuildIRQ(0x22)

BuildIRQ(0x23)

BuildIRQ(0x24)

BuildIRQ(0x25)

BuildIRQ(0x26)

BuildIRQ(0x27)

BuildIRQ(0x28)

BuildIRQ(0x29)

BuildIRQ(0x2a)

BuildIRQ(0x2b)

BuildIRQ(0x2c)

BuildIRQ(0x2d)

BuildIRQ(0x2e)

BuildIRQ(0x2f)

BuildIRQ(0x30)

BuildIRQ(0x31)

BuildIRQ(0x32)

BuildIRQ(0x33)

BuildIRQ(0x34)

BuildIRQ(0x35)

BuildIRQ(0x36)

BuildIRQ(0x37)

/**
 * 这里是SMP使用的
 */
BuildIRQ(0xc8)

BuildIRQ(0xc9)

BuildIRQ(0xca)

BuildIRQ(0xcb)

BuildIRQ(0xcc)

BuildIRQ(0xcd)

BuildIRQ(0xce)

BuildIRQ(0xcf)

BuildIRQ(0xd0)

BuildIRQ(0xd1)

// 定义中断函数地址数组，函数由宏函数来的
void (*interrupt[24])(void)=
        {
                IRQ0x20Interrupt,
                IRQ0x21Interrupt,
                IRQ0x22Interrupt,
                IRQ0x23Interrupt,
                IRQ0x24Interrupt,
                IRQ0x25Interrupt,
                IRQ0x26Interrupt,
                IRQ0x27Interrupt,
                IRQ0x28Interrupt,
                IRQ0x29Interrupt,
                IRQ0x2aInterrupt,
                IRQ0x2bInterrupt,
                IRQ0x2cInterrupt,
                IRQ0x2dInterrupt,
                IRQ0x2eInterrupt,
                IRQ0x2fInterrupt,
                IRQ0x30Interrupt,
                IRQ0x31Interrupt,
                IRQ0x32Interrupt,
                IRQ0x33Interrupt,
                IRQ0x34Interrupt,
                IRQ0x35Interrupt,
                IRQ0x36Interrupt,
                IRQ0x37Interrupt,
        };

void (*SMPInterrupt[10])(void)=
        {
                IRQ0xc8Interrupt,
                IRQ0xc9Interrupt,
                IRQ0xcaInterrupt,
                IRQ0xcbInterrupt,
                IRQ0xccInterrupt,
                IRQ0xcdInterrupt,
                IRQ0xceInterrupt,
                IRQ0xcfInterrupt,
                IRQ0xd0Interrupt,
                IRQ0xd1Interrupt,
        };


IRPDescriptorTable interruptDescriptor[NR_IRQS] = {0};

IRPDescriptorTable SMPIPIDesc[10] = {0};

int registerIRQ(unsigned long irq,
                void *arg,
                void (*handler)(unsigned long nr, unsigned long parameter, PerformSite *regs),
                unsigned long parameter,
                hwInterruptController *controller,
                char *irqName) {

    // 1. 因为本系统为 I/O APIC 分配的中断向量号为 32-55，所以中断号必须要减去32才能找到 interruptDescriptor 数组中对应的索引
    IRPDescriptorTable *p = &interruptDescriptor[irq - 32];

    // 2. 设置 IRPDescriptorTable 的成员变量
    p->controller = controller;
    p->irqName = irqName;
    p->parameter = parameter;
    p->flags = 0;
    p->handler = handler;

    // 3. 执行安装函数
    p->controller->install(irq, arg);

    // 4. 开启中断处理函数
    p->controller->enable(irq);

    return 1;
}


int unregisterIRQ(unsigned long irq) {

    // 1. 找到对应的中断处理描述符表
    IRPDescriptorTable *p = &interruptDescriptor[irq - 32];

    // 2. 禁止中断处理函数
    p->controller->disable(irq);

    // 3. 卸载中断处理函数
    p->controller->uninstall(irq);

    // 4. 设置 IRPDescriptorTable 成员变量的属性
    p->controller = NULL;
    p->irqName = NULL;
    p->parameter = NULL;
    p->flags = 0;
    p->handler = NULL;

    return 1;
}


int registerIPI(unsigned long irq,
                void (*handler)(unsigned long nr, unsigned long parameter, PerformSite *regs),
                unsigned long parameter,
                hwInterruptController *controller,
                char *irqName) {

    IRPDescriptorTable *p = &SMPIPIDesc[irq - 200];

    p->controller = NULL;
    p->irqName = irqName;
    p->parameter = parameter;
    p->flags = 0;
    p->handler = handler;

    return 1;
}

int unRegisterIPI(unsigned long irq) {

    IRPDescriptorTable *p = &SMPIPIDesc[irq - 200];

    p->controller = NULL;
    p->irqName = NULL;
    p->parameter = NULL;
    p->flags = 0;
    p->handler = NULL;

    return 1;
}